import Mock from "mockjs";

// 模拟狗狗列表数据
const dogList = [
    {
        id: "01",
        kind: "金毛",
        listing: true,
        hot: 450,
        views: 800,
        creater: "管理员一",
        time: "2024-11-01",
    },
    {
        id: "02",
        kind: "拉布拉多",
        listing: false,
        hot: 320,
        views: 600,
        creater: "管理员二",
        time: "2024-11-02",
    },
    {
        id: "03",
        kind: "哈士奇",
        listing: true,
        hot: 510,
        views: 920,
        creater: "管理员三",
        time: "2024-11-03",
    },
    {
        id: "04",
        kind: "萨摩耶",
        listing: false,
        hot: 300,
        views: 400,
        creater: "管理员一",
        time: "2024-11-04",
    },
    {
        id: "05",
        kind: "柴犬",
        listing: true,
        hot: 430,
        views: 720,
        creater: "管理员二",
        time: "2024-11-05",
    },
    {
        id: "06",
        kind: "阿拉斯加",
        listing: true,
        hot: 520,
        views: 1000,
        creater: "管理员三",
        time: "2024-11-06",
    },
    {
        id: "07",
        kind: "边牧",
        listing: false,
        hot: 275,
        views: 300,
        creater: "管理员一",
        time: "2024-11-07",
    },
    {
        id: "08",
        kind: "比熊",
        listing: true,
        hot: 340,
        views: 480,
        creater: "管理员二",
        time: "2024-11-08",
    },
    {
        id: "09",
        kind: "贵宾",
        listing: true,
        hot: 290,
        views: 650,
        creater: "管理员三",
        time: "2024-11-09",
    },
    {
        id: "10",
        kind: "约克夏",
        listing: false,
        hot: 310,
        views: 500,
        creater: "管理员一",
        time: "2024-11-10",
    },
    {
        id: "11",
        kind: "柯基",
        listing: true,
        hot: 405,
        views: 780,
        creater: "管理员二",
        time: "2024-11-11",
    },
    {
        id: "12",
        kind: "法国斗牛犬",
        listing: true,
        hot: 370,
        views: 550,
        creater: "管理员三",
        time: "2024-11-12",
    },
    {
        id: "13",
        kind: "巴哥",
        listing: false,
        hot: 280,
        views: 440,
        creater: "管理员一",
        time: "2024-11-13",
    },
    {
        id: "14",
        kind: "腊肠犬",
        listing: true,
        hot: 360,
        views: 670,
        creater: "管理员二",
        time: "2024-11-14",
    },
    {
        id: "15",
        kind: "秋田犬",
        listing: true,
        hot: 415,
        views: 820,
        creater: "管理员三",
        time: "2024-11-15",
    },
    {
        id: "16",
        kind: "大白熊",
        listing: false,
        hot: 245,
        views: 390,
        creater: "管理员一",
        time: "2024-11-16",
    },
    {
        id: "17",
        kind: "杜宾",
        listing: true,
        hot: 405,
        views: 730,
        creater: "管理员二",
        time: "2024-11-17",
    },
    {
        id: "18",
        kind: "藏獒",
        listing: false,
        hot: 500,
        views: 980,
        creater: "管理员三",
        time: "2024-11-18",
    },
    {
        id: "19",
        kind: "雪纳瑞",
        listing: true,
        hot: 385,
        views: 600,
        creater: "管理员一",
        time: "2024-11-19",
    },
    {
        id: "20",
        kind: "北京犬",
        listing: true,
        hot: 345,
        views: 540,
        creater: "管理员二",
        time: "2024-11-20",
    }
];




// 获取狗狗列表接口
Mock.mock("/dog/list", "post", (config) => {
    const req = JSON.parse(config.body)
    return {
        code: 1,
        message: "获取狗狗列表成功",
        data: {
            dogList: dogList.slice((req.count - 1) * req.pageSize, req.count * req.pageSize),
            length: dogList.length
        }
    };
});

// 添加狗狗接口
Mock.mock("/dog/add", "post", (config) => {
    const req = JSON.parse(config.body)
    dogList.push(req)
    return {
        code: 200,
        message: '添加数据成功',
    }
});

// 删除狗狗接口
Mock.mock("/dog/delete", "post", (config) => {
    const req = JSON.parse(config.body)

    const res = {
        code: 200,
        message: '删除数据失败'
    }

    for (let i = 0; i < dogList.length; i++) {
        if (dogList[i].id == req.id) {
            dogList.splice(i, 1)
            res.message = '数据删除成功'
            break
        }
    }

    return res
});

// 更新狗狗接口
Mock.mock("/dog/edit", "post", (config) => {
    const req = JSON.parse(config.body)

    const res = {
        code: 200,
        message: '修改数据失败'
    }

    for (let i = 0; i < dogList.length; i++) {
        if (dogList[i].id == req.id) {
            dogList[i] = req
            res.message = '数据修改成功'
            break
        }
    }

    return res
});


// 查找狗狗接口
Mock.mock('/dog/query', 'post', (config) => {
    const req = JSON.parse(config.body);

    const res = {
        code: 200,
        message: '查询数据失败',
    };

    const arr = [];

    const kind = req.kind || ''; // 通过 req.kind 进行查询

    for (let i = 0; i < dogList.length; i++) {
        // 确保 dogList[i].kind 存在，并检查 req.kind 是否为空
        if (dogList[i].kind && dogList[i].kind.indexOf(kind) !== -1) {
            // 找到数据了
            res.message = '数据查询成功';
            arr.push(dogList[i]);
        }
    }

    res.dogList = arr;
    res.length = arr.length;

    return res;
});


